package commons.ihm.courbe;

import commons.drawing.UtilsDrawing;
import commons.utils.primitive_geom.Point_d;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.SwingUtilities;


public class PanelCourbe extends javax.swing.JPanel {

	private int nbPointBetweenAutomaticRepaint;
	private int nbPointUntilNextAutomaticRepaint;
	public void setNbPointBetweenAutomaticRepaint(int nb) {
		this.nbPointBetweenAutomaticRepaint=nb;
		nbPointUntilNextAutomaticRepaint=nb;
	}
	
	private double xMin, xMax, yMin, yMax;

	private ArrayList<Courbe> vCourbe;

    public PanelCourbe() {
        initComponents();
		vCourbe = new ArrayList<Courbe>();
		nbPointBetweenAutomaticRepaint = 1;
		nbPointUntilNextAutomaticRepaint = nbPointBetweenAutomaticRepaint;
		xMin = 0;
		xMax = 1;
		yMin = 0;
		yMax = 1;
    }

	
	public void configureDisplay(double xMin, double xMax, double yMin, double yMax) {
		assert (xMin<xMax && yMin<yMax);
		this.xMin=xMin;
		this.xMax=xMax;
		this.yMin=yMin;
		this.yMax=yMax;
	}


	public void addCourbe (Color color) {
		vCourbe.add(new Courbe(color));
	}

	
	//public Courbe getCourbe (int numCourbe) {return vCourbe.get(numCourbe);}
	public void addPoint(Point_d pt, int numCourbe) {
		Courbe courbe = vCourbe.get(numCourbe);

		//pour eviter le dessin qui lit en meme temps que l'ajout d'un point
		synchronized(courbe) {
			courbe.addPoint(pt);
		}
		nbPointUntilNextAutomaticRepaint--;
		if (nbPointUntilNextAutomaticRepaint<=0) {
			nbPointUntilNextAutomaticRepaint = nbPointBetweenAutomaticRepaint;
			repaint();
		}
	}

	@Override
	public void paint(Graphics g) {
		//System.out.println("REPAINT____________________________");
		prepareForPainting();
		Graphics2D g2d_offScreen = (Graphics2D) offgc;
		clearScreen(g2d_offScreen);

		UtilsDrawing.Quality(g2d_offScreen);

		double largPanel = this.getWidth();
		double hautPanel = this.getHeight();

		double xL, yL;
		Ellipse2D.Double e = new Ellipse2D.Double();

		for (Courbe courbe : vCourbe) {

			//pour eviter le dessin qui lit en meme temps que l'ajout d'un point
			synchronized(courbe) {
				
				g2d_offScreen.setColor(courbe.color);

				for (Point_d pt : courbe.vPoints) {

					xL = largPanel * (pt.x - xMin) / (xMax - xMin);
					yL = hautPanel - hautPanel * (pt.y - yMin) / (yMax - yMin);

					e.x = xL;
					e.y = yL;
					e.width = 1.5;
					e.height = 1.5;

					g2d_offScreen.draw(e);
				}
			}
		}

		g.drawImage(offscreen, 0, 0, this);
	}


	// Variable pour affichage dans un bitmap
	private Graphics offgc = null;
	private Image offscreen = null;
	private Dimension currentPanelDimension;

	private void prepareForPainting() {
		Dimension newDimension = getSize();

		if (offscreen==null || !currentPanelDimension.equals(newDimension)) {
			currentPanelDimension = newDimension;
			offscreen = createImage(currentPanelDimension.width, currentPanelDimension.height);
			offgc = offscreen.getGraphics();
		}
	}

	private void clearScreen(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
	}


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
